//
//  GroupMenbersVC.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/6/1.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "GroupMenbersVC.h"
#import "MicroDef.h"
#import "GroupMenberCell.h"
#import "CXmppRequestMgr.h"
#import "GroupMemberDetailVC.h"
#import "UIUtil.h"
#import "CContactMgr.h"
#import "CContactModel.h"
#import "CGroupMemberModel.h"
#import "CGroupMemberMgr.h"
#import "NSArray+PinYin.h"
#import "CDepartmentMgr.h"
#import "CDepartmentModel.h"
#import "UIImageView+WebCache.h"
#import "CFolderMgr.h"
#import "MyInformationVC.h"
#import "MBProgressHUD+WZ.h"
#import "IMSearchDisplayController.h"
#import "CGroupMgr.h"
#import "UIBarButtonItem+category.h"
#import "WZNavigationController.h"
#import "EFengDisCussCreateController.h"

#define KSECTIONHEIGHT 35.0f
@interface GroupMenbersVC ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>
{
    UITableView *_tableView;
    UISearchBar *_searchBar;
    UISearchDisplayController *_searchDisplayController;
    NSMutableArray *_allFirstLetter;
    NSMutableArray *_sectionListArray;
    NSMutableArray *_searchAllFirstLetter;
    NSMutableArray *_searchSectionListArray;
}
@property (nonatomic, strong) NSMutableArray *memeberArray;

@property (nonatomic, strong) CGroupMemberMgr *groupMemMgr;

@property (nonatomic, strong) CContactMgr *conMgr;

@property (nonatomic, strong) CGroupMgr *groupMgr;
StrongProperty CContactModel *curContact;

@end

@implementation GroupMenbersVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (_searchDisplayController.active) {
        [_searchDisplayController setActive:NO];
    }
    
}


- (NSMutableArray *)memeberArray
{
    if (_memeberArray == nil) {
        _memeberArray = [NSMutableArray array];
    }
    return _memeberArray;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPGETGROUPMEMBERS object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPDELDISCUSSMEM object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPGETDISCUSSIONMEMLIST object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPADDDISCUSSIONMEM object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:k_XMPPGETDISCUSSIONLIST object:nil];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (IsIOS7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }

    // Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    if (self.group.groupType == 1) {
         [self.navigationItem setNewTitleCenter:@"组成员"];
    } else {
         [self.navigationItem setNewTitleCenter:@"群成员"];
    }
    
    if (self.group.groupType == 1) {
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"btn-newDiscussion" title:nil target:self sel:@selector(addNewDisMem) titleFont:Font18 titleColor:WhiteColor];
    }
    
    _tableView = [[UITableView alloc] init];
    _tableView.frame = CGRectMake(0, 44.0f, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 44.0f - 64.0f);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.sectionIndexColor = RGBCOLOR(0x64, 0x64, 0x64);
    _tableView.sectionIndexBackgroundColor = ClearColor;
    _tableView.sectionIndexTrackingBackgroundColor = ClearColor;
    _tableView.sectionHeaderHeight = 20.0f;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.groupMemMgr = [[CGroupMemberMgr alloc] init];
    self.conMgr = [[CContactMgr alloc] init];
    self.groupMgr = [[CGroupMgr alloc] init];
    
    self.curContact = [self.conMgr loadCurContact];
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
     _searchBar.backgroundImage = [UIImage imageNamed:@"BG-search"];
    _searchBar.placeholder = @"搜索";
    _searchBar.delegate = self;
    // 添加 searchbar 到 headerview
    [self.view addSubview:_searchBar];
    
    _searchDisplayController = [[IMSearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    
    // searchResultsDataSource 就是 UITableViewDataSource
    _searchDisplayController.searchResultsDataSource = self;
    // searchResultsDelegate 就是 UITableViewDelegate
    _searchDisplayController.searchResultsDelegate = self;
    _searchDisplayController.searchResultsTableView.sectionIndexColor = RGBCOLOR(0x64, 0x64, 0x64);
     _searchDisplayController.searchResultsTableView.sectionIndexBackgroundColor = ClearColor;
     _searchDisplayController.searchResultsTableView.sectionIndexTrackingBackgroundColor = ClearColor;
    _searchDisplayController.delegate = self;
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didGetGroupMembers:) name:K_XMPPGETGROUPMEMBERS object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(delMem) name:K_XMPPDELDISCUSSMEM object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didGetGroupMembers:) name:K_XMPPGETDISCUSSIONMEMLIST object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didGetGroupMembers:) name:k_XMPPGETDISCUSSIONLIST object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addMem) name:K_XMPPADDDISCUSSIONMEM object:nil];
    
    _allFirstLetter = [NSMutableArray arrayWithCapacity:5];
    _sectionListArray = [NSMutableArray arrayWithCapacity:5];
    
    _searchAllFirstLetter = [NSMutableArray arrayWithCapacity:5];
    _searchSectionListArray = [NSMutableArray arrayWithCapacity:5];
    
    if ([CXmppMgr shareInstance].reachability.currentReachabilityStatus != NotReachable) {
        if (self.group) {
            
            NSArray *array = [self.groupMemMgr loadAllGroupMemberByGroupID:self.group.GroupId];
            
            if (array.count == self.group.UserNum) {
                NSMutableArray *arrayM = [NSMutableArray array];
                for (CGroupMemberModel *groupModel in array) {
                    CContactModel *contact = [self.conMgr loadContactWithUserId:groupModel.UserId];
                    if (contact) {
                        if (contact.isDel == 1) {
                            [arrayM addObject:contact];
                        } else {
                            [self.groupMemMgr delGroupMember:groupModel];
                        }
                    }
                }
               [self.memeberArray addObjectsFromArray:arrayM];
                self.group.UserNum = arrayM.count;
                [self.groupMgr updateGroup:self.group];
                [self handleContactArray:arrayM];
            } else {
                
                [[CXmppRequestMgr shareInstance] getGroupMembersWithGroup:self.group];
                [UIUtil addMBProgressViewTitle:@"加载中..." inView:self.view alpha:1.0 failureTitle:@"加载失败"];
            }
            
        }
    } else {
        [MBProgressHUD showError:@"加载失败"];
    }
    
    
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
}


- (void)delMem
{
    [MBProgressHUD showSuccess:@"删除讨论组成员成功！" toView:self.view];
    
}

- (void)addMem
{
    [MBProgressHUD showSuccess:@"已成功添加讨论组成员！" toView:self.view];
    NSArray *array = [self.groupMemMgr loadAllGroupMemberByGroupID:self.group.GroupId];
    NSMutableArray *arrayM = [NSMutableArray array];
    for (CGroupMemberModel *groupModel in array) {
        CContactModel *contact = [self.conMgr loadContactWithUserId:groupModel.UserId];
        if (contact) {
            if (contact.isDel == 1) {
                [arrayM addObject:contact];
            } else {
                [self.groupMemMgr delGroupMember:groupModel];
            }
        }
    }
    [self.memeberArray addObjectsFromArray:arrayM];
    self.group.UserNum = arrayM.count;
    [self.groupMgr updateGroup:self.group];
    [self handleContactArray:arrayM];
    [_tableView reloadData];

}

- (void)addNewDisMem
{
    //bug fixed by 9511
    if (self.group.UserNum >= 50) {
        [MBProgressHUD showError:@"已达50人上限"];
        return;
    }
    if (_sectionListArray.count > 0) {
        
        [[[CDepartmentMgr alloc] init] updateAllDeptNotSelected:nil];
        [[[CContactMgr alloc] init] updateAllContactNotSelected:nil];
        EFengDisCussCreateController *cre = [[EFengDisCussCreateController alloc] init];
        cre.group = self.group;
        WZNavigationController *nav = [[WZNavigationController alloc] initWithRootViewController:cre];
        [self presentViewController:nav animated:YES completion:nil];
    } else {
        [MBProgressHUD showError:@"正在加载讨论组成员..."];
    }

}


- (void)didGetGroupMembers:(NSNotification *)notice
{
    [UIUtil hideMBProgressViewForView:self.view];
    
    NSArray *array = [self.groupMemMgr loadAllGroupMemberByGroupID:self.group.GroupId];
    NSMutableArray *arrayM = [NSMutableArray array];
    for (CGroupMemberModel *groupModel in array) {
        CContactModel *contact = [self.conMgr loadContactWithUserId:groupModel.UserId];
        if (contact) {
            if (contact.isDel == 1) {
                [arrayM addObject:contact];
            } else {
                [self.groupMemMgr delGroupMember:groupModel];
            }
        }
    }
    [self.memeberArray addObjectsFromArray:arrayM];
    self.group = [self.groupMgr loadGroup:self.group.GroupId];
    self.group.UserNum = arrayM.count;
    [self.groupMgr updateGroup:self.group];
    [self handleContactArray:arrayM];
    [_tableView reloadData];

}


- (void)handleContactArray:(NSArray *)contacArray
{
    
    _allFirstLetter = [NSArray IndexArray:contacArray];
    _sectionListArray = [NSArray LetterSortArray:contacArray];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if(tableView == _tableView)
    {
//        NSArray *row1 = [_sectionListArray objectAtIndex:section];
        NSArray * row = _sectionListArray[section];
        return row.count;
    }
    else
    {
//    NSArray *row2 = [_searchSectionListArray objectAtIndex:section];
        NSArray *row = _searchSectionListArray[section];
        return row.count;
    }
   
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView              // Default is 1 if not implemented
{
    if(tableView == _tableView)
    {
        return [_sectionListArray count];
    }
    else
    {
        return [_searchSectionListArray count];
    }
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(tableView == _tableView)
    {
        NSArray *rowArray = _sectionListArray[indexPath.section];
        CContactModel *contact = rowArray[indexPath.row];
        static NSString *NormalCellIdentifier = @"GroupMenberCell";
        GroupMenberCell *groupMemberCell = [tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];
        if (groupMemberCell == nil) {
            groupMemberCell = [[[NSBundle mainBundle] loadNibNamed:@"GroupMenberCell" owner:self options:nil] objectAtIndex:0];
        }
        [groupMemberCell.icon sd_setImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:contact.UserID]] placeholderImage:[UIImage imageNamed:@"80-80"]];
        //bug 8692 fixed by lixu
        NSString *titleText = [NSString stringWithFormat:@"%@",contact.UserName];
        //bug 9349 fixed by lixu
        if (contact.Position.length >0) {
            titleText = [NSString stringWithFormat:@"%@-%@",titleText,contact.Position];
        }
        [groupMemberCell.titleLabel setText:titleText];
        NSInteger rows = [tableView numberOfRowsInSection:indexPath.section];
        if(indexPath.row == rows - 1)
        {
            [groupMemberCell.seperatorLine setHidden:YES];
        } else {
            groupMemberCell.seperatorLine.hidden = NO;
        }
        return groupMemberCell;
    }
    else
    {
        
        NSArray *rowArray = _searchSectionListArray[indexPath.section];
        CContactModel *contact = rowArray[indexPath.row];
        static NSString *NormalCellIdentifier = @"GroupMenberCell";
        GroupMenberCell *groupMemberCell = [tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];
        if (groupMemberCell == nil) {
            groupMemberCell = [[[NSBundle mainBundle] loadNibNamed:@"GroupMenberCell" owner:self options:nil] objectAtIndex:0];
        }
        [groupMemberCell.icon sd_setImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:contact.UserID]] placeholderImage:[UIImage imageNamed:@"80-80"]];
        //bug 8692 fixed by lixu
        //bug 9349 fixed by lixu

        NSString *titleText = [NSString stringWithFormat:@"%@",contact.UserName];
                if (contact.Position.length >0) {
            titleText = [NSString stringWithFormat:@"%@-%@",titleText,contact.Position];
        }
        [groupMemberCell.titleLabel setText:titleText];
        
        
        NSInteger rows = [tableView numberOfRowsInSection:indexPath.section];
        if(indexPath.row == rows - 1)
        {
            [groupMemberCell.seperatorLine setHidden:YES];
        } else {
            groupMemberCell.seperatorLine.hidden = NO;
        }
        return groupMemberCell;
    }
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (_tableView == tableView) {
        return _allFirstLetter;
    } else{
        return _searchAllFirstLetter;
    }
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSInteger count = 0;
    if(tableView == _tableView)
    {
        count = index;
       
    } else {
        count = index;
    }
    return count;
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    

    CGFloat height = 0;
//    switch (section) {
//        case 1:
//            height = KSECTIONHEIGHT;
//            break;
//        case 2:
//            height = KSECTIONHEIGHT;
//            break;
//        case 3:
//            height = KSECTIONHEIGHT;
//            break;
//        default:
//            break;
//    }
    if(tableView == _tableView)
    {
        height = KSECTIONHEIGHT;
    } else {
        height = KSECTIONHEIGHT;
    }
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0;
    return height;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    UIView *headerView = nil;
//    if(section == 0)
//    {
//        return headerView;
//    }
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
//    UIView *beginLine = [[UIView alloc] init];
//    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
//    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
//    [headerView addSubview:beginLine];
    
//    UIView *endLine = [[UIView alloc] init];
//    endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
//    [endLine setBackgroundColor:KSEPERATORLINECOLOR];
//    [headerView addSubview:endLine];
    
    UILabel *label = [[UILabel alloc] init];
//    NSString *string = [NSString stringWithFormat:@"%@ (%d人)", [_allFirstLetter objectAtIndex:(section)], [[_sectionListArray objectAtIndex:(section)] count]];
//    [label setText:string];
    [label setTextColor:RGBCOLOR(0x92, 0x92, 0x92)];
    [label setFont:[UIFont systemFontOfSize:14]];
    label.frame = CGRectMake(15, 0, 200, KSECTIONHEIGHT);
    if (_tableView == tableView) {
        
        label.text = _allFirstLetter[section];
    } else {
        label.text = _searchAllFirstLetter[section];
    }
    [headerView addSubview:label];
    
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
{
    UIView *headerView = nil;
    if(section != 1)
    {
        return headerView;
    }
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 2);
    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    UIView *beginLine = [[UIView alloc] init];
    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:beginLine];
    return headerView;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 60.0f;
    
//    if(indexPath.row == 0 && indexPath.section == 0)
//    {
//        height = 60.0f;
//    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (_tableView == tableView) {
        NSArray *rowArray = _sectionListArray[indexPath.section];
        CContactModel *contact = rowArray[indexPath.row];
        CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
        [self showMembersDetailVCWithMemeber:contact deptName:dep.DepartName];

    } else {
        NSArray *rowArray = _searchSectionListArray[indexPath.section];
        CContactModel *contact = rowArray[indexPath.row];
        CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
        [self showMembersDetailVCWithMemeber:contact deptName:dep.DepartName];
    }
    
    
    
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *rowArray = _sectionListArray[indexPath.section];
    CContactModel *contact = rowArray[indexPath.row];
    
    if (self.group.groupType == 1 && [self.group.OwnerId isEqualToString:self.curContact.UserID] && ![self.curContact.UserID isEqualToString:contact.UserID]) {
        return UITableViewCellEditingStyleDelete;
    } else {
        return UITableViewCellEditingStyleNone;
    }
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSArray *rowArray = _sectionListArray[indexPath.section];
        CContactModel *contact = rowArray[indexPath.row];
        [_groupMemMgr delGroupMemberWithUserId:contact.UserID groupId:self.group.GroupId];
        [[CXmppRequestMgr shareInstance] delDiscussMembersWithGroup:self.group contact:contact];
        NSArray *array = [self.groupMemMgr loadAllGroupMemberByGroupID:self.group.GroupId];
        NSMutableArray *arrayM = [NSMutableArray array];
        for (CGroupMemberModel *groupModel in array) {
            CContactModel *contact = [self.conMgr loadContactWithUserId:groupModel.UserId];
            if (contact) {
                if (contact.isDel == 1) {
                    [arrayM addObject:contact];
                } else {
                    [self.groupMemMgr delGroupMember:groupModel];
                }
            }
        }
        [self.memeberArray addObjectsFromArray:arrayM];
        self.group.UserNum = arrayM.count;
        [self.groupMgr updateGroup:self.group];
        [self handleContactArray:arrayM];
        [_tableView reloadData];

    }
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _tableView) {
    NSArray *rowArray = _sectionListArray[indexPath.section];
    CContactModel *contact = rowArray[indexPath.row];

    if (self.group.groupType == 1 && [self.group.OwnerId isEqualToString:self.curContact.UserID] && ![self.curContact.UserID isEqualToString:contact.UserID])
    {
        return YES;
    } else {
        return NO;
    }
    }
    else
    {
        return NO;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
{
    return @"删除";
}



-(void)showMembersDetailVCWithMemeber:(CContactModel *)contact deptName:(NSString *)depName
{
        if (![[[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userId isEqualToString:contact.UserID]) {
        
       GroupMemberDetailVC *groupMemberDetail = [[GroupMemberDetailVC alloc] init];
        groupMemberDetail.member = contact;
            groupMemberDetail.isFromGroupChat = YES;
            groupMemberDetail.isFromChat = NO;
        [self.navigationController pushViewController:groupMemberDetail animated:YES];

    }else {
        
        MyInformationVC *myInfoDetail = [[MyInformationVC alloc] init];
        myInfoDetail.curuser = contact;
        [self.navigationController pushViewController:myInfoDetail animated:YES];
    }
    

    
    
}




#pragma mark-- UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar                     // called when text starts editing
{
    searchBar.showsCancelButton = YES;
    for(id cc in [_searchDisplayController.searchBar subviews])
    {
        if([cc isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)cc;
            [btn setTitle:@"取消"  forState:UIControlStateNormal];
        }
    }
    
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [_searchSectionListArray removeAllObjects];
    [_searchAllFirstLetter removeAllObjects];
    NSArray *contactArray = [[[CContactMgr alloc] init] searchContactWithSeachStr:searchText];
    if (contactArray.count > 0) {
        NSMutableArray *searchArrayM = [NSMutableArray array];
        for (CContactModel *contact in contactArray) {
            for (CContactModel *groupModel in self.memeberArray) {
                if ([contact.UserID isEqualToString:groupModel.UserID]) {
                    [searchArrayM addObject:contact];
                }
            }
        }
    
        _searchAllFirstLetter = [NSArray IndexArray:searchArrayM];
        _searchSectionListArray = [NSArray LetterSortArray:searchArrayM];
    }
    else
    {
        return;
    }
    
    }

#pragma mark-- UISearchDisplayDelegate
//@protocol UISearchDisplayDelegate <NSObject>
//
//@optional
//
//// when we start/end showing the search UI
//- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0);
//- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0);
//- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0);
//- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0);
//
//// called when the table is created destroyed, shown or hidden. configure as necessary.
//- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView NS_DEPRECATED_IOS(3_0,8_0);
//- (void)searchDisplayController:(UISearchDisplayController *)controller willUnloadSearchResultsTableView:(UITableView *)tableView NS_DEPRECATED_IOS(3_0,8_0);
//
//// called when table is shown/hidden
//- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView NS_DEPRECATED_IOS(3_0,8_0);
//- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView NS_DEPRECATED_IOS(3_0,8_0);
//- (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView NS_DEPRECATED_IOS(3_0,8_0);
//- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView NS_DEPRECATED_IOS(3_0,8_0);
//
//// return YES to reload table. called when search string/option changes. convenience methods on top UISearchBar delegate methods
//- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString NS_DEPRECATED_IOS(3_0,8_0);
//- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption NS_DEPRECATED_IOS(3_0,8_0);
//
//@end
//隐藏搜索为空时cell的分割线
//bug 8510 fixed by lixu
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView NS_DEPRECATED_IOS(3_0,8_0){
    [tableView setContentInset:UIEdgeInsetsZero];
    [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{ for(UIView *subview in controller.searchResultsTableView.subviews) {
    
    if([subview isKindOfClass:[UILabel class]]) {
        
        [(UILabel*)subview setText :@"未找到匹配的信息"];
        
    }
    
}
    return YES;
}


- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0)
{
    UIView *topView = controller.searchBar.subviews[0];
    
    for (UIView *subView in topView.subviews) {
        
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            
            UIButton * cancelButton = (UIButton*)subView;
            
            [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            
        }
        
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView == tableView) {
        cell.transform =  CGAffineTransformMakeTranslation(HARDWARE_SCREEN_WIDTH, 0);
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:0 animations:^{
            cell.transform = CGAffineTransformIdentity;
        } completion:NULL];
    }
   
}

@end
